package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {
    /**
     * 频道列表分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listByName(ChannelDto dto) {
//        构建分页对象
        IPage iPage = new Page(dto.getPage(), dto.getSize());
//        构建查询条件
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isEmpty(dto)) {
            wrapper.like(WmChannel::getName, dto.getName());
        }
//        执行查询
        this.page(iPage, wrapper);
//        封装分页结果
        return new PageResponseResult(dto.getPage(),dto.getSize(),iPage.getTotal(),iPage.getRecords());
    }

    /**
     * 保存频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(WmChannel::getName,wmChannel.getName());
        int count = this.count(wrapper);
        if (count == 0 ){
        wmChannel.setIsDefault(false);
        wmChannel.setStatus(false);
        wmChannel.setOrd(1);
        this.save(wmChannel);
        return ResponseResult.okResult();
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
    }
    /**
     *  编辑频道
     * @param wmChannel
     * @return
     */

    @Override
    public ResponseResult chsngeStutas(WmChannel wmChannel) {

        LambdaUpdateWrapper<WmChannel> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(WmChannel::getId,wmChannel.getId());
        WmChannel wmChannel1 = new WmChannel();
        wmChannel1.setId(wmChannel.getId());
        if (wmChannel.getDescription() != null){
            wmChannel1.setDescription(wmChannel.getDescription());
        }
        if (wmChannel.getName() != null){
            wmChannel1.setName(wmChannel.getName());
        }
        if (wmChannel.getOrd() != null){
            wmChannel1.setOrd(wmChannel.getOrd());
        }
        if (wmChannel.getStatus() != null){
            wmChannel1.setStatus(wmChannel.getStatus());
        }
//        wrapper.setEntity(wmChannel1);
        this.updateById(wmChannel1);
        return ResponseResult.okResult();
    }


}
